package com.lframework.xingyun.basedata.impl.unit;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.PageResultUtil;
import com.lframework.starter.web.core.components.resp.PageResult;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.common.utils.ObjectUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.Unit;
import com.lframework.xingyun.basedata.mappers.UnitMapper;
import com.lframework.xingyun.basedata.service.unit.UnitService;
import com.lframework.xingyun.basedata.vo.unit.CreateUnitVo;
import com.lframework.xingyun.basedata.vo.unit.QueryUnitVo;
import com.lframework.xingyun.basedata.vo.unit.UpdateUnitVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UnitServiceImpl extends BaseMpServiceImpl<UnitMapper, Unit> implements UnitService {

    @Override
    public PageResult<Unit> queryPage(Integer pageIndex, Integer pageSize, QueryUnitVo vo) {

        Assert.greaterThanZero(pageIndex);
        Assert.greaterThanZero(pageSize);

        IPage<Unit> page = this.getBaseMapper().queryPage(vo);

        return PageResultUtil.convert(page);
    }

    @Override
    public List<Unit> queryList(QueryUnitVo vo) {

        return getBaseMapper().queryList(vo);
    }

    @Cacheable(value = Unit.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public Unit findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增商品单位，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String create(CreateUnitVo vo) {

        Unit data = new Unit();
        data.setId(IdUtil.getId());
        data.setUnitType(vo.getUnitType());
        data.setUnitName(vo.getUnitName());
        data.setDisplayNum(vo.getDisplayNum());

        getBaseMapper().insert(data);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);

        return data.getId();
    }

    @OpLog(type = OtherOpLogType.class, name = "修改商品单位，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UpdateUnitVo vo) {

        Unit data = getBaseMapper().selectById(vo.getId());
        if (ObjectUtil.isNull(data)) {
            throw new DefaultClientException("商品单位不存在！");
        }

        LambdaUpdateWrapper<Unit> updateWrapper = Wrappers.lambdaUpdate(Unit.class)
                .set(Unit::getUnitType, vo.getUnitType())
                .set(Unit::getUnitName, vo.getUnitName())
                .set(Unit::getDisplayNum, vo.getDisplayNum())
                .eq(Unit::getId, vo.getId());

        getBaseMapper().update(updateWrapper);

        OpLogUtil.setVariable("id", data.getId());
        OpLogUtil.setExtra(vo);
    }

    @CacheEvict(value = Unit.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

